package com.attendance.controller;

import com.attendance.mapper.HomeMapper;
import com.attendance.result.InitMenu;
import com.attendance.security.user.pojo.UserInfo;
import com.attendance.security.user.service.UserService;
import com.attendance.service.MenuServiceImpl;
import com.attendance.service.NoticeServiceImpl;
import com.attendance.service.UserDetailsServiceImpl;
import com.attendance.spring.SpringMvcUtils;
import com.attendance.spring.SpringSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Home控制器
 *
 * @author 鵺丶
 * @date 2021/03/12
 */
@Controller
@RequestMapping()
public class HomeController {

    @Autowired
    NoticeServiceImpl noticeService;
    @Autowired
    MenuServiceImpl menuService;
    @Autowired
    HomeMapper homeMapper;
    @Autowired
    UserService userService;
    @Autowired
    UserDetailsServiceImpl userDetailsService;

    @GetMapping()
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("index");
        UserInfo userInfo = userService.selectByName(SpringSecurityUtils.getUserName());
        modelAndView.addObject("userName", userDetailsService.selectById(userInfo.getUserId()).getName());
        modelAndView.addObject("userId", userInfo.getUserId());
        return modelAndView;
    }

    @GetMapping("/home")
    public ModelAndView home() {
        ModelAndView modelAndView = new ModelAndView("/system/home/home");
        modelAndView.addObject("notices", noticeService.getList());
        modelAndView.addObject("userNum", homeMapper.getUserNum());
        modelAndView.addObject("departNum", homeMapper.getDepartNum());
        modelAndView.addObject("dLog", homeMapper.getDateLog());
        modelAndView.addObject("mLog", homeMapper.getMonthLog());
        return modelAndView;
    }

    @GetMapping("/login")
    public static String login() {
        return "/system/home/login";
    }

    @ResponseBody
    @GetMapping("/initMenu")
    public InitMenu initMenu() {
        InitMenu initMenu = new InitMenu("首页", "/home"
                , "考勤系统", "/images/logo.png", "", menuService.getMenus());
        return initMenu;
    }
}
